#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
   @Author: 叶建伟
   @Email: yejw46507@hundsun.com
   @Time: 2023/7/5 13:58
   @File: threadInsertBTA.py
"""
import threading
import queue
from queue import Empty

from DataTest.databases import MysqlUtil, DaMengUtil
from DataTest.sqlmontage.SQLMontage import get_sql_key, montage_sql_value, get_sql
from DataTest.sqlmontage.clientInfo import client_info_list, client_info_srm, cst_unit_list, bank_txn_list, \
    securities_acc_list, capital_acc_list, risk_new_list, risk_his_list, settle_type, multi_bank_txn_list, deliver_list, \
    sus_report_list, tran_his_list
import datetime

# 定义全局变量num_threads线程数量，num_inserts插入数据总量，batch_size单次插入数量

num_threads = 20
num_inserts = 10000
batch_size = 50
table_name = 'samlps_63_tran_his'

processes = []

# 定义队列用于存储拼接好的SQL语句
sql_queue = queue.Queue()

# 定义锁，用于保证线程安全
lock = threading.Lock()

BLOCK=True


# 定义线程执行的函数：SQL拼接
def sql_assemble(thread_id):
    now = datetime.datetime.now()
    formatted_datetime = now.strftime("%m%d%H")
    temp = formatted_datetime + '00000000'
    table_value = get_sql(table=table_name)
    table_key = get_sql_key(table=table_name)
    for i in range(num_inserts//batch_size//num_threads):
        sql = table_key
        for j in range(batch_size):
            # sql_list = cst_unit_list()
            # sql_list = securities_acc_list()
            # sql_list = risk_his_list()
            # sql_list = settle_type()
            # sql_list = bank_txn_list()
            sql_list = tran_his_list()
            # sql_list = sus_report_list()
            id = str(thread_id * (num_inserts // num_threads) + i * batch_size + j)
            client_id = temp[:14-len(id)]+id
            #sql_list.update({'client_id':client_id,'client_name':"机构"+client_id,'bid':id})
            # temp1 = 20000000+int(id)
            temp1 =id
            if int(id)>6000000:
                sql_list.update({'client_id': client_id, 'client_name': "机构" + client_id,'id_no':client_id, 'bid': temp1})
            elif int(id)>8000000:
                sql_list.update({'client_id': client_id, 'client_name': "产品" + client_id,'id_no':client_id, 'bid': temp1})
            else:
                sql_list.update({'client_id': client_id, 'client_name': "个人" + client_id,'id_no':client_id,'bid': temp1})
            # sql_list.update({'financtal_organ_code': id, 'business_name': "测试" + id,'bid':id})
            values = montage_sql_value(table=table_value, sql_list=sql_list)
            sql += values
            if j < batch_size - 1:
                sql += ","
        # lock.acquire()
        # print(f'{thread_id}拼装sql: {sql}\n')
        sql_queue.put(sql)
        # lock.release()


# 定义线程执行的函数：SQL插入
def sql_insert(db):
    # while not sql_queue.empty():
    global sql
    while True:
        lock.acquire(blocking=True,timeout=-1)
        try:
            sql = sql_queue.get(block=BLOCK)
            # print(f'插入sql:{sql}\n')
            # 执行SQL插入操作
        except Empty as e:
            break
        finally:
            lock.release()
        if sql:
            db.execsql(sql)
        else:
            break





if __name__ == '__main__':
    print(datetime.datetime.now())
    # 创建并启动SQL拼接线程
    assemble_threads = []
    for i in range(num_threads):
        thread = threading.Thread(target=sql_assemble, args=(i,))
        thread.start()
        assemble_threads.append(thread)


   # 创建并启动SQL插入线程
    insert_threads = []
    db = DaMengUtil('hssamlps', 'hssamlps', '10.20.192.117')

    for i in range(num_threads):
        thread = threading.Thread(target=sql_insert,args=(db,))
        thread.start()
        insert_threads.append(thread)

    #等待所有SQL拼接线程执行完毕
    for thread in assemble_threads:
        thread.join()
    else:
        BLOCK=False
        sql_queue.put(False)

    # # 等待所有SQL插入线程执行完毕
    # for thread in insert_threads:
    #     thread.join()
    print(datetime.datetime.now())
